<?php
namespace app\api\controller;
use think\Controller;
use app\Response;
use QL\QueryList;
use \Curl\Curl;
use app\api\controller\book\Biquge;
use app\api\controller\book\Fengqing;
use app\api\controller\book\Pashuwang;
use app\common;
use think\Db;
use think\Config;

class Txt {
	public function index(){
		var_dump('hhhh');die;
		set_time_limit(0);
		$key	=	isset($_GET['key'])?$_GET['key']:'';
		$step_Str =	isset($_GET['step'])?$_GET['step']:'';
		$step 	= 	!empty($step_Str)?explode(',', $step_Str):array(3);
		$url 	=	'';

		$webModule =	isset($_GET['webModule'])?$_GET['webModule']:'Fengqing';
		
		$book_link_id 	=	isset($_GET['txt_id'])?$_GET['txt_id']:'';
		$chapter_link_id=	'';

		//$key = '一念永恒';
		switch ($webModule) {
			case 'Fengqing':
				$web = new Fengqing();
				break;

			case 'Pashuwang':
				$web = new Pashuwang();
				break;	
			
			default:
				$web = new Biquge();
				break;
		}

		$result = $web->index($key, $url, $step, $book_link_id, $chapter_link_id);
		Response::show(200,'ok',$result);
	}
	/**
	 * [getList 获取列表]
	 * @return [type] [description]
	 */
	public function getList(){

		$info = db('story')->select();

		Response::show(200,'ok',$info);
	}
	/**
	 * [getChapterList 获取章节列表]
	 * @return [type] [description]
	 */
	public function getChapterList(){
		$txt_id = isset($_GET['txt_id'])?$_GET['txt_id']:'';
		$info = array();
		$code = 200; //  默认章节已完成下载
		if($txt_id){
			//  小说章节总数
			$count = db('story_chapter_link')->where('txt_id', $txt_id)->count();
			//  已下载章节信息
			$info = db('story_chapter')->where('txt_id', $txt_id)->select();
			//  已下载章节数量
			$total = count($info);
			if($count > $total){
				$code = 202;
			}
		}
		Response::show($code,'ok',$info);
	}
	/**
	 * [getWebList 获取可下载小说的网络模块]
	 * @return [type] [description]
	 */
	public function getWebList(){
		$info = db('web')->select();

		Response::show(200,'ok',$info);
	}
	/**
	 * [getSearchList 获取搜索结果]
	 * @return [type] [description]
	 */
	public function getSearchList(){
		$info = db('web_search')->select();

		Response::show(200,'ok',$info);
	}
	/**
	 * [delSearchData 删除搜索结果]
	 * @return [type] [description]
	 */
	public function delSearchData(){
		$id = isset($_GET['id'])?$_GET['id']:'';
		//有id就按id删除，没有id就全删
		if($id){
			$info = db('web_search')->where('id',$id)->delete();
		}else{
			$info = Db::table('web_search')->where('id','>','0')->delete();
		}
		
		Response::show(200,'ok',$info);
	}
	/**
	 * [copySearchData 搜索的小说名称等信息录入]
	 * @return [type] [description]
	 */
	public function copySearchData(){
		$id = isset($_GET['id'])?$_GET['id']:'';
		$code = -200;
		$msg  = '录入失败!';

		if($id){
			$info = db('web_search')->where('id',$id)->find();
			if($info){
				$name 	= isset($info['name'])?$info['name']:'';
				$author = isset($info['author'])?$info['author']:'';
				$link 	= isset($info['link'])?$info['link']:'';
				$web 	= isset($info['web'])?$info['web']:'';

				$time = date('Y-m-d H:i:s');
				//插入小说数据表
				$story_data = array(
					'name' 			=> $name,
					'author' 		=> $author,
					'create_time' 	=> $time,
					'update_time' 	=> $time
					);
				$story_key = array('name', 'author');
				$txt_id = common::addTbl('story', $story_data, $story_key);

				if($txt_id){
					//插入小说来源信息表
					$book_info = array(
						'txt_id' 		=> $txt_id,
						'link' 			=> $link,
						'web' 			=> $web,
						'create_time' 	=> $time,
						'update_time' 	=> $time
					);
					$book_key = array('txt_id');
					$book_link_id = common::addTbl('story_book_link', $book_info, $book_key);
					if($book_link_id){
						$code = 200;
						$msg  = '录入成功!';
					}
				}
			}
		}
		Response::show($code, $msg, $id);
	}
	/**
	 * [createTxt 小说下载]
	 * @return [type] [description]
	 */
	public function createTxt(){
		set_time_limit(0);
		$txt_id = isset($_GET['txt_id'])?$_GET['txt_id']:'';

		if($txt_id){
			$story = Db::table('story')->where('txt_id',$txt_id)->find();
			if(!$story){
				Response::show(404,'书籍不存在!');exit;
			}
			$book_name  = isset($story['name'])?$story['name']:'无名小说M';
			$author 	= isset($story['author'])?$story['author']:'无名M';
			$file_name  = $book_name.'.txt';

			$story_path = Config::get('story_path');
			$path = isset($story_path['root_dir'])?$story_path['root_dir']:'';
			$web  = isset($story_path['web'])?$story_path['web']:'';

			//$file = $path.$file_name;
			$file_path = "../../uploads/story/";
			$file = $file_path.$file_name;

			$system = common::system();
			if($system == 'windows'){
				$file = common::encoding($file, "GBK", "UTF-8");
			}

			$path = common::createDirectory($path);

			if(file_exists($file)){
				@unlink($file);
			}
			$last_id = 0;
			while ($chapter_info = Db::table('story_chapter')->where('txt_id',$txt_id)->where('id','>',$last_id)->order('chapter_id asc')->limit(10)->select()) {
				foreach ($chapter_info as $key => $val) {
					$id = isset($val['id'])?$val['id']:0;
					$last_id = $id;

					$chapter_name = isset($val['chapter_name'])?$val['chapter_name']:'';
					$content 	  = isset($val['content'])?$val['content']:'';
					$content 	  = common::replacetxt($content);
					if($content){
						$content = $chapter_name.PHP_EOL.$content;
						$story 	 = file_put_contents($file, $content.PHP_EOL.PHP_EOL, FILE_APPEND);				
					}
				}
			}
			Response::show(200,'下载成功!');exit;
			
		}
		Response::show(201,'下载失败!');
	}



	public function microtime_float(){

	   list($usec, $sec) = explode(" ", microtime());

	   return ((float)$usec + (float)$sec);

	}

	public function getMillisecond() {
		list($t1, $t2) = explode(' ', microtime());
		return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
	}

}